<?php
session_start();

// 生成4位随机数字
$code = rand(1000, 9999);
$_SESSION['captcha'] = $code;

// 创建图片
$image = imagecreatetruecolor(100, 30);
$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 0);

// 填充背景
imagefilledrectangle($image, 0, 0, 100, 30, $bg);

// 添加干扰线
for($i = 0; $i < 3; $i++) {
    $linecolor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, 100), rand(0, 30), rand(0, 100), rand(0, 30), $linecolor);
}

// 添加干扰点
for($i = 0; $i < 100; $i++) {
    $pixelcolor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, 100), rand(0, 30), $pixelcolor);
}

// 写入验证码
imagestring($image, 5, 25, 8, $code, $textcolor);

// 输出图片
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?> 